import 'package:encrypt/encrypt.dart';
import 'package:pointycastle/asymmetric/api.dart';
import 'rsaUtils.dart';
import 'dart:convert';

void main() {
  //testAes();
  //testRsa();
  //test();
  //testAesForBase64();
  testRsaBase64();
}

void test(){
  Encrypted encrypted=Encrypted.fromBase64("MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCB");
  encrypted.base64;
  print(encrypted.base64);
}
void testAesForBase64(){
  final plainText = '中华人民共和国';
  final key=Key.fromBase64("a7SDfrdDKRBe5FaN2n3Gfg==");
  final encrypter = Encrypter(AES(key,mode: AESMode.ecb));
  final encrypted=encrypter.encrypt(plainText,);
  print(encrypted.base64);
  final decrypted=encrypter.decrypt64(encrypted.base64,);
  print(decrypted);
}
void testAes() {
  final plainText = '中华人民共和国';
  final key = Key.fromUtf8('MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCB');
  final iv = IV.fromLength(16);

  final encrypter = Encrypter(AES(key));

  final encrypted = encrypter.encrypt(plainText, iv: iv);
  final decrypted = encrypter.decrypt(encrypted, iv: iv);
  print("==============aes=============");
  print(
      "decrypted:$decrypted"); // Lorem ipsum dolor sit amet, consectetur adipiscing elit
  print(
      "encrypted:$encrypted"); // R4PxiU3h8YoIRqVowBXm36ZcCeNeZ4s1OvVBTfFlZRdmohQqOpPQqD1YecJeZMAop/hZ4OxqgC1WtwvX/hP9mw==
  print(
      "encrypted.base64:${encrypted.base64}"); // R4PxiU3h8YoIRqVowBXm36ZcCeNeZ4s1OvVBTfFlZRdmohQqOpPQqD1YecJeZMAop/hZ4OxqgC1WtwvX/hP9mw==
}

void testRsa() {
  String pubK =
      "-----BEGIN RSA PUBLIC KEY-----\nMIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQDE+l8NDPsPLpuWx9ggEY4ICATlhP06QCuC2ubBHKgq4OcJVtur1in6vAvKultUIwwqMW/478jKHyt3Ptgtq5Rtx3mTdrRDPlMSDsgqOxQl5w3LZzjVM3WHzBRKzUBGB9fMUJI3ubX7TtE3kTj3zc152nm0VIpZ3R8CugHFOqnf3QIDAQAB";
  String praK =
      "MIICdwIBADANBgkqhkiG9w0BAQEFAASCAmEwggJdAgEAAoGBAKmlMDqirWHU8nkTlfipPXj5ju19UFVfTk/Nlpd04iJCxjM2tRPaalz/9r8shWiCj/xx/2YX3NfFAa7XcZzP95nqxqq12AVPzFTpRzHaulg6+ODNC6sMavy8QMz4Y3+6rD/c4Mzd3/x9cv77mkfp/yHAhrAz5Iks96EEzhMyqgPbAgMBAAECgYBqTGiZBSARIibA+SXKxvkO2YjWhOQWan6egJrw0jtz7lVU992k0AI/vd910MbTQqRIMYIwDZnC1d4FGVhGQwXItelaHdSLsoFl3v/qQ9okkLnfPepYF9bCW1dmt1bwYlDQQj8OvwIn506L9rYcH6Ay9543eOqFhT445lTkjVk+6QJBANHMUs2ulZ4v1BHUgwBB+f6i18CYkXApGulsJovcsKLDQCrmtPM48YfuB2BcDNOIpZQ9/befKntP3n6dLhi41ncCQQDPATArjCeqZeJ7iPFr9Qom7PxwQ9vMhz2YC512Be22m7yt3EnjWmPNrMtv0QMG4DKmbQwWDttenRFP//1hn0K9AkEAz00awJvywPS3ksquivGmSUK091yJ0Q78xYsU53wwmRNe80U1pWMt3ceISGGMVvSdz8YVTnwaqO/5hJhhNRJm7wJAfjoQQI2/ZkWofIH+DUGnxiK+sEqAzlAte/ihNOXalX6T6ESq/asRrspOZ0nBmcC+wgM+2niGxkPq2aiUvqGZ1QJBAL28pFvdQHe+kRI/QotsKKaWtd1ukxwFlx8RyW/ZMjTaL+SopeqBd8BjzcWYa1gkYYrasV3re2s/nik/hU6IPUU=";
  String pubKString = utf8.decode(base64.decode(praK),allowMalformed: true);
  print("pubKString:${pubKString}");

  final parser = RSAKeyParser();
  final RSAPublicKey publicKey = parser.parse(pubK);
  final RSAPrivateKey privateKey = parser.parse(praK);
  final plainText = 'Lorem ipsum dolor sit amet, consectetur adipiscing elit';
  final encrypter =
      Encrypter(RSA(publicKey: publicKey, privateKey: privateKey));

  final encrypted = encrypter.encrypt(plainText);
  final decrypted = encrypter.decrypt(encrypted);

  print(decrypted); // Lorem ipsum dolor sit amet, consectetur adipiscing elit
  print(encrypted.base64);
}
void testRsaBase64(){
  String pubK='''-----BEGIN PUBLIC KEY-----
MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAsLMRxGAoNN4pKl050CUJrnzYUL7v+C/S
Ac7LhPe3jxr6/hViN2Z91sS92yPdQ+du8zlqqzwl7oyQtr/sBYWFIKHmxGUuIMws3EGlgJBXqCF6
ISIWU2o02FTmZeMdrPgCGPnmzgoRPR6e9sGblIynrdIyhv8Xf9yuQZgfZA8VOcyqJj64G1tFrcUb
MkWVv/fEXqRxGFdJewWf7UrqGgZKdi0jwV9wpSMhVUUJfwyHKj04azpVkEwunnK2Ysvll/2ey2JT
KwioWO2leYcgs4EVpeSB9Yk5F04OS/fyCIKJeWUS9y8zfRrx0JGo3WviojtTz69XO+lM2avST0cY
KTq8WwIDAQAB
''';

  String priK='''-----BEGIN PRIVATE KEY-----
MIIEvgIBADANBgkqhkiG9w0BAQEFAASCBKgwggSkAgEAAoIBAQCwsxHEYCg03ikqXTnQJQmufNhQ
vu/4L9IBzsuE97ePGvr+FWI3Zn3WxL3bI91D527zOWqrPCXujJC2v+wFhYUgoebEZS4gzCzcQaWA
kFeoIXohIhZTajTYVOZl4x2s+AIY+ebOChE9Hp72wZuUjKet0jKG/xd/3K5BmB9kDxU5zKomPrgb
W0WtxRsyRZW/98RepHEYV0l7BZ/tSuoaBkp2LSPBX3ClIyFVRQl/DIcqPThrOlWQTC6ecrZiy+WX
/Z7LYlMrCKhY7aV5hyCzgRWl5IH1iTkXTg5L9/IIgol5ZRL3LzN9GvHQkajda+KiO1PPr1c76UzZ
q9JPRxgpOrxbAgMBAAECggEBAJ6W2Wdq9isdCc1gBrdtxpa4rl0qQa57HP1KYRdiTXk/Oe45uwEG
GANeaRf5xGlhmg7qhXZScaj8uBWNg4QhJi73oGq8dP4yw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Qlg7MOkEbGUKkVYfzoXy2bXza04xae
wSmAmyWAyZXOyVxgBqhyOrgB4wNh
''';
  String str="中华人民共和国";
  RsaKeyHelper rsaKeyHelper=RsaKeyHelper.instance;
  RSAPublicKey rsaPublicKey = rsaKeyHelper.parsePublicKeyFromPem(pubK);
  RSAPrivateKey rsaPrivateKey = rsaKeyHelper.parsePrivateKeyFromPem(priK);
  //公钥加密
  String cipher=rsaKeyHelper.encrypt2Hex(str, rsaPublicKey);

  print("cipher:\n$cipher");
  //私钥解密
  String plain=rsaKeyHelper.decryptFromHex(cipher, rsaPrivateKey);
  print("plain:$plain");

}
